﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BUS;
using DATA.BusinessObjects;
using DATA.ManagerObjects;

namespace Presentation
{
    public partial class frmLapPhieuThuePhong : Form
    {
        private int intSTT = 1;
        public frmLapPhieuThuePhong()
        {
            InitializeComponent();
        }

        private void frmLapPhieuThuePhong_Load(object sender, EventArgs e)
        {
            try
            {
                IList<Phong> lstPhong = PhongBUS.LayDanhSachPhong();
                if (lstPhong != null)
                {
                    cbo_TenPhong.DataSource = lstPhong;
                    cbo_TenPhong.DisplayMember = "TenPhong";
                    cbo_TenPhong.ValueMember = "Id";
                }
                txt_DonGiaThue.Text = "100000";
                DataGridViewComboBoxColumn dgvcbocolumn = (DataGridViewComboBoxColumn)dgv_KhachHang.Columns["tenLoaiKhach"];
                IList<Loaikhach> lstLoaiKhach = LoaiKhachBUS.LayDanhSachLoaiKhach();
                if (lstLoaiKhach != null)
                {
                    dgvcbocolumn.DataSource = lstLoaiKhach;
                    dgvcbocolumn.DisplayMember = "TenLoaiKhach";
                    dgvcbocolumn.ValueMember = "Id";
                }
            }
            catch (Exception ex)
            {   
                throw new Exception("Lỗi ở form load", ex);
            }
        }

        private void cbo_TenPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void btn_LapPhieuThuePhong_Click(object sender, EventArgs e)
        {
            Phieuthuephong phieuThuePhong = new Phieuthuephong();
            String maPhong = cbo_TenPhong.SelectedValue.ToString();
            String donGiaThue = txt_DonGiaThue.Text;
            float DonGiaThue = float.Parse(donGiaThue); // Don gia thue kieu float
            DateTime ngayBatDauThue = dtp_NgayBatDauThue.Value;
            DateTime ngayTraPhong = dtp_NgayTraPhong.Value;

            // Khoi tao doi tuong Phong, set/get thuoc tinh, roi moi moi add vao phieuThuePhong
            Phong phong = new Phong();
            phong.Id = maPhong;
            phong.TenPhong = cbo_TenPhong.Text;
            phieuThuePhong.Phong = phong;   // Thêm phòng vào phiếu thuê phòng

            Phieuthuephong ptpCuoiCung = PhieuThuePhongBUS.LayPhieuThuePhongCuoiCung();
            string maPhieuThuePhong = ThamSoBUS.TaoMaTangTuDong(ptpCuoiCung.Id, 4, "MPTP");
            phieuThuePhong.Id = maPhieuThuePhong;
            phieuThuePhong.DonGiaThue = DonGiaThue;
            phieuThuePhong.NgayThuePhong = ngayBatDauThue;
            phieuThuePhong.NgayTraPhong = ngayTraPhong;

            int iRows = dgv_KhachHang.Rows.Count;
            if (iRows != 0)
            {
                int t = int.Parse(KhachHangBUS.LayKhachHangMoiNhat().Id.Substring(3));  // Lấy mã khách hàng cuối cùng
                t++;
                for (int i = 0; i < iRows - 1; i++)
                {
                    String tenKhachHang = "";
                    String loaiKhach = "ND";
                    String cmnd = "";
                    String diaChi = "";
                    if (dgv_KhachHang.Rows[i].Cells["tenKhach"].Value != null)
                    {
                        tenKhachHang = dgv_KhachHang.Rows[i].Cells["tenKhach"].Value.ToString();
                    }
                    if (dgv_KhachHang.Rows[i].Cells["tenLoaiKhach"].Value != null)
                    {
                        loaiKhach = dgv_KhachHang.Rows[i].Cells["tenLoaiKhach"].Value.ToString();
                    }
                    if (dgv_KhachHang.Rows[i].Cells["cmnd"].Value != null)
                    {
                        cmnd = dgv_KhachHang.Rows[i].Cells["cmnd"].Value.ToString();
                    }
                    if (dgv_KhachHang.Rows[i].Cells["diaChi"].Value != null)
                    {
                        diaChi = dgv_KhachHang.Rows[i].Cells["diaChi"].Value.ToString();
                    }
                    Khachhang kh = new Khachhang();
                    kh.Id = "MKH" + t;
                    t++;
                    kh.TenKhachHang = tenKhachHang;
                    Loaikhach objLoaiKhach = LoaiKhachBUS.LayLoaiKhachTheoID(loaiKhach);
                    kh.Loaikhach = objLoaiKhach;
                    kh.Cmnd = cmnd;
                    kh.DiaChi = diaChi;
                    phieuThuePhong.Khachhangs.Add(kh);  // Thêm khách hàng vào phiếu thuê phòng
                }
            }
            // Thêm phiếu thuê phòng
            bool flag = PhieuThuePhongBUS.LapPhieuThuePhong(phieuThuePhong);
            if (flag == true)
            {
                MessageBox.Show("Lập phiếu thuê phòng thành công");
            }
            else
            {
                MessageBox.Show("Không lập phiếu thuê phòng được");
            }
        }

        private void dgv_KhachHang_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow dgvRow = dgv_KhachHang.CurrentRow;
            dgvRow.Cells["stt"].Value = intSTT;
            intSTT++;
        }

        private void btn_TimPhong_Click(object sender, EventArgs e)
        {
            frmTraCuuPhong frm = new frmTraCuuPhong();
            frm.Show();
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_LamMoi_Click(object sender, EventArgs e)
        {
            dgv_KhachHang.Rows.Clear();
        }
    }
}
